package com.ruoyi.project.wf.sys.api.impl;

import cn.hutool.core.lang.Dict;
import com.ruoyi.project.system.domain.SysPost;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.service.ISysPostService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.wf.sys.api.DeptApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangyuxi
 * @date 2025/5/24 22:52
 */
@Slf4j
@Component
public class DeptApiImpl implements DeptApi {
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private ISysPostService iSysPostService;
    @Override
    public Dict findById(Long id) {
        // String leaderIds = dict.getStr("leaderIds");
        // 查找部门经理,暂时
        // 递归的找到本机构及所有上级机构
        String sql =  "WITH RECURSIVE cte AS (    " +
                "  SELECT dept_id, parent_id, dept_name    " +
                "  FROM sys_dept    " +
                "  WHERE dept_id = ?  " +
                "  UNION ALL    " +
                "  SELECT t.dept_id, t.parent_id, t.dept_name    " +
                "  FROM sys_dept t    " +
                "  JOIN cte ON cte.parent_id = t.dept_id)    " +
                "SELECT dept_id, parent_id, dept_name    " +
                "FROM cte;";
        List<Map<String, Object>> depts = jdbcTemplate.queryForList(sql, id);
        System.out.println("depts:"+depts);
        log.info("本机构及上级机构为:{}", depts);
        String deptStr = depts.stream().map(t->"'"+t.get("dept_id")+"'").collect(Collectors.joining(","));
        log.info("deptStr:{}", deptStr);
        // 根据post_code找post_id
        SysPost sysPostCond = new SysPost();
        sysPostCond.setPostCode("leader");
        List<SysPost> sysPosts = iSysPostService.selectPostList(sysPostCond);
        Long postId = sysPosts.get(0).getPostId();
        // 查询岗位是leader的用户
        sql = "select a.* from sys_user a,sys_user_post b,sys_dept c where a.dept_id=c.dept_id " +
                " and a.user_id=b.user_id and b.post_id=? and c.dept_id in("+deptStr+")";
        List<Map<String, Object>> users = jdbcTemplate.queryForList(sql, postId);
        log.info("users:{}", users);
        String leaderNames = users.stream().map(t -> t.get("user_name").toString()).collect(Collectors.joining(","));
        log.info("leaderNames:{}", leaderNames);

        sysPostCond.setPostCode("boss");
        sysPosts = iSysPostService.selectPostList(sysPostCond);
        postId = sysPosts.get(0).getPostId();
        users = jdbcTemplate.queryForList(sql, postId);
        log.info("users:{}", users);
        String mainLeaderNames = users.stream().map(t -> t.get("user_name").toString()).collect(Collectors.joining(","));
        log.info("mainLeaderNames:{}", mainLeaderNames);
        Dict dict = Dict.create();
        dict.put("leaderNames",leaderNames);
        dict.put("mainLeaderNames", mainLeaderNames);
        return dict;
    }
}
